<template>
	<view class="add_box">
		<u-empty v-if="list.length==0"></u-empty>
		<view class="" v-else>
			<Address @handleItem='handleItem(item)' @edit='handleEdit(item)' @delete='handleDel(item.id)'
				v-for="(item,index) in list" :key='item.id' :item='item'></Address>
		</view>
		<view class="sub_btn">
			<u-button type="primary" @click="handNavTo">新增地址</u-button>

		</view>
	</view>
</template>

<script>
	import Address from '@/components/ZY-address/ZY-address.vue'
	export default {
		components: {
			Address
		},
		data() {
			return {
				list: [],
				addressId: '',
				details: ""
			};
		},
		mounted() {
			this.init()
		},
		onLoad(e) {
			if (e && e.addressId) {
				this.addressId = e.addressId
				this.details = e.details
			}
		},
		methods: {
			handleItem(item) {
				// 如果存在addressId 说明是从订单页面点进来的.
				if (this.addressId) {
					uni.navigateTo({
						url: '/pages/submitOrder/submitOrder?details=' + this.details + '&address=' + JSON
							.stringify(item)
					})
				}
			},
			handNavTo(item) {
				uni.navigateTo({
					url: `/pages/address/addAddress?addressId=${this.addressId}&goodsDetails=${this.details}`
				})
			},
			handleEdit(item) {
				uni.navigateTo({
					url: `/pages/address/addAddress?addressId=${this.addressId}&goodsDetails=${this.details}&details=${JSON.stringify(item)}`
				})
			},
			handleDel(id) {

				const that = this
				uni.showModal({
					title: '提示!',
					content: '是否删除?',
					success(res) {
						if (res.confirm) {
							that.$u.api.delReceivingAddress({
								id
							}).then(del => {
								uni.showToast({
									icon: 'none',
									title: '删除成功'
								})
								that.init()
							})
						}

					}
				})
			},
			init() {
				this.$u.api.getReceivingAddress().then(res => {
					this.list = res
					console.log(this.list)
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	.add_box {
		padding-bottom: 200rpx;
	}

	.sub_btn {
		position: fixed;
		bottom: 30rpx;
		left: 0;
		width: 100%;
		padding: 0 30rpx;
	}
</style>
